﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainCarRentingApplication : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private HrCarRentingEntity mSelectedCarRenting;
        #endregion

        #region Constructor
        public frmMaintainCarRentingApplication(HrCarRentingEntity entity)
        {
            InitializeComponent();
            mSelectedCarRenting = entity;
        }

        public frmMaintainCarRentingApplication(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }

        public frmMaintainCarRentingApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            txtPersonNameCn.Text = "";
            cmbStatus.SelectedIndex = -1;
            txtMobile.Text = "";
            txtDepartment.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.PersonCode;
            txtPersonNameCn.Text = mSelectedPerson.NameCn;
            cmbStatus.SetSelectedValue(mSelectedPerson.Status);
            txtMobile.Text = mSelectedPerson.Mobile;
            MstDepartmentEntity department = MstDepartmentBs.GetMstDepartmentByID(mSelectedPerson.DepartmentId);
            if (department != null)
            {
                txtDepartment.Text = department.DepartmentName;
            }
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", false);
        }

        private void ResetTotalMileage()
        {
            if (string.IsNullOrEmpty(txtStartMileage.Text) || string.IsNullOrEmpty(txtEndMileage.Text)
                || CommonFunctions.ToDecimal(txtStartMileage.Text) == 0 || CommonFunctions.ToDecimal(txtEndMileage.Text) == 0)
            {
                txtTotalMileage.Text = "";
                return;
            }
            txtTotalMileage.Text = (CommonFunctions.ToDecimal(txtEndMileage.Text) - CommonFunctions.ToDecimal(txtStartMileage.Text)).ToString("0.00");
        }

        private void SetFields()
        {
            if (mSelectedCarRenting == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedCarRenting.PersonId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedCarRenting.PersonId);
                SetPersonFields();
            }
            txtPersonCount.Text = mSelectedCarRenting.PersonCount.ToString();
            if (mSelectedCarRenting.StartDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedCarRenting.StartDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpStartDate.EditValue = mSelectedCarRenting.StartDate;
            }
            if (mSelectedCarRenting.EndDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedCarRenting.EndDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpEndDate.EditValue = mSelectedCarRenting.EndDate;
            }
            cmbStatus.SetSelectedValue(mSelectedCarRenting.Status);
            txtStartMileage.Text = mSelectedCarRenting.StartMileage.ToString("#0.00");
            txtEndMileage.Text = mSelectedCarRenting.EndMileage.ToString("#0.00");
            txtDestination.Text = mSelectedCarRenting.Destination;
            txtRentingReason.Text = mSelectedCarRenting.RentingReason;
            txtCarNo.Text = mSelectedCarRenting.CarNo;
            txtApprovedRemark.Text = mSelectedCarRenting.ApproveRemark;
            txtRemark.Text = mSelectedCarRenting.Remark;

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedCarRenting.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedCarRenting.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedCarRenting.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedCarRenting.LastModifiedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, mSelectedCarRenting.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, mSelectedCarRenting.ApprovedBy);

            ResetTotalMileage();
        }

        private void SetEntity()
        {
            if (mSelectedCarRenting == null)
            {
                mSelectedCarRenting = new HrCarRentingEntity();
                mSelectedCarRenting.RentingId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedCarRenting.PersonId = mSelectedPerson.PersonId;
                mSelectedCarRenting.DepartmentId = mSelectedPerson.DepartmentId;
            }
            mSelectedCarRenting.PersonCount = CommonFunctions.ToInt32(txtPersonCount.Text);
            if (dtpStartDate.EditValue!=null)
            {
                mSelectedCarRenting.StartDate = Convert.ToDateTime(dtpStartDate.EditValue);
            }
            else
            {
                mSelectedCarRenting.StartDate = Convert.ToDateTime("1900-01-01 00:00");
            }
            if (dtpEndDate.EditValue!=null)
            {
                mSelectedCarRenting.EndDate = Convert.ToDateTime(dtpEndDate.EditValue);
            }
            else
            {
                mSelectedCarRenting.EndDate = Convert.ToDateTime("1900-01-01 00:00");
            }
            mSelectedCarRenting.Status = "STATUS02";
            mSelectedCarRenting.StartMileage = CommonFunctions.ToDecimal(txtStartMileage.Text);
            mSelectedCarRenting.EndMileage = CommonFunctions.ToDecimal(txtEndMileage.Text);
            mSelectedCarRenting.Destination = txtDestination.Text;
            mSelectedCarRenting.RentingReason = txtRentingReason.Text;
            mSelectedCarRenting.CarNo = txtCarNo.Text;
            mSelectedCarRenting.ApproveRemark = txtApprovedRemark.Text;
            mSelectedCarRenting.Remark = txtRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedCarRenting.CreatedDate = DateTime.Now;
                mSelectedCarRenting.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedCarRenting.LastModifiedDate = DateTime.Now;
                mSelectedCarRenting.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }

        private bool CheckData()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtPersonCount.Text))
            {
                MessageBox.Show("随行人数不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpStartDate.EditValue == null)
            {
                MessageBox.Show("出发时间不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtDestination.Text))
            {
                MessageBox.Show("目的地不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtRentingReason.Text))
            {
                MessageBox.Show("用车事由不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = HrCarRentingBs.InsertHrCarRenting(mSelectedCarRenting);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = HrCarRentingBs.UpdateHrCarRenting(mSelectedCarRenting);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    mSelectedCarRenting.ApprovedDate = DateTime.Now;
                    mSelectedCarRenting.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = HrCarRentingBs.UpdateHrCarRenting(mSelectedCarRenting);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintainCarRentingApplication_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetPersonFields();
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedCarRenting == null)
            {
                return;
            }
            SetEntity();
            mSelectedCarRenting.Status = "STATUS03";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Reject()
        {
            if (mSelectedCarRenting == null)
            {
                return;
            }
            mSelectedCarRenting.Status = "STATUS04";
            mSelectedCarRenting.ApproveRemark = txtApprovedRemark.Text;
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                mSelectedPerson = frm.SelectedPerson;
                SetPersonFields();
            }
            else
            {
                mSelectedPerson = null;
                SetPersonFields();
            }
        }

        private void txtStartMileage_Leave(object sender, EventArgs e)
        {
            ResetTotalMileage();
        }

        private void txtEndMileage_Leave(object sender, EventArgs e)
        {
            ResetTotalMileage();
        }
        #endregion
    }
}
